<template>
  <div>
    <Header></Header> <!-- 子页面公共头部 -->
    <div class="ej_box">
      <div class="ej_center">
        <Crumbs lmtitle="搜索结果"></Crumbs> <!-- 面包屑导航 -->
        <div class="clear"></div>
        <div class="row">

          <div class="col-md-3">
            <SeniorSearch @search="search" :formdata="formdata"></SeniorSearch> <!-- 高级搜索 -->
          </div>
          <div class="col-md-9">
            <!-- 搜索结果 -->
            <div class="ej_content_box">
              <div class="ej_content">
                <div class="ej_content_nav">
                  <div class="ej_content_nav_pc">
                    <h2 class="ej_content_nav_pc_h2"><span>搜索结果页</span></h2>

                    <div class="clearfix"></div>
                  </div>
                </div>

                <div class="ej_content_editor">
                  <div class="zcfg_con">
                    <div class="zcfg_li2">
                      <div class="zcfg_licon zcfg_licon_cobnt">
                        <div class="noresult" v-if="searchList.length==0">
                          无结果
                        </div>
                        <div class="resultlist">
                          <ul v-if="searchList.length>0">
                            <li v-for="item in searchList" @click="open(item.id)">
                              <div>
                                <i class="iconfont icon-fangkuai"></i>
                                <p v-html="item.html"></p>
                                <img src="../assets/images/new.png" class="icon_new" v-if="item.newContent==1">
                                    <img src="../assets/images/top.png" class="icon_top" v-if="item.topContent==1">
                              </div>
                              <span>
                                {{item.createTime}}
                              </span>
                            </li>
                          </ul>
                        </div>
                        <div class="pgepagination" v-if="searchList.length!=0">
                          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
                            :current-page="formdata.pageNum" :page-sizes="[10, 20, 30, 50]"
                            :page-size="formdata.pageSize" layout="total, sizes, prev, pager, next, jumper"
                            :total="formdata.total">
                          </el-pagination>
                        </div>
                      </div>
                    </div>

                  </div>


                </div>
              </div>
            </div>
          </div>

          <div class="clearfix"></div>
        </div>


      </div>
    </div>
    <Foot></Foot> <!-- 网站公共底部 -->
  </div>
</template>
<script>
import Foot from "@/components/foot.vue";
import Crumbs from "@/components/crumbs.vue";
import SeniorSearch from "@/components/seniorsearch.vue";
import Cookies from 'js-cookie'
import { globalQueryAll,globalQueryAllContent } from "@/api/webviews";
import Header from "@/components/header.vue";
export default {
  name: "searchresult",
  components: {
    Header,
    Crumbs,
    SeniorSearch,
    Foot
  },
  data() {
    return {
      isClickSearch: false,
      dhData: [],
      searchList: [],
      ParentId: '',
      formdata2: {
        pageNum: 1,
        pageSize: 10,
        param:'',
      },
      formdata: {
        pageNum: 1,
        pageSize: 10,
        total: 0,
        title: '',
        content: '',
        author: '',
        postedBy: '',
        organization: '',
        beginTime: '',
        endTime: '',
      }
    };
  },
  created() {
    this.dhData = JSON.parse(Cookies.get('navdata'))[this.$route.query.navId]
    if (this.$route.query.title) {
      this.formdata2.param = this.$route.query.title
      this.formdata.title = this.$route.query.title
      this.search2(this.formdata2)
    }
    if (this.$route.query.formdata) {
      this.formdata = JSON.parse(this.$route.query.formdata)
      this.search(this.formdata)
    }
  },
  watch: {
	    //监听路由地址的改变
	    $route:{
	        immediate:true,
	        handler(){
            if (this.$route.query.title) {
            this.formdata2.param = this.$route.query.title
            this.search2(this.formdata2)
          }
	        }
	    }
	},
  methods: {
    search2(val) {
      globalQueryAllContent(val).then(res => {
        // console.log('res地址栏搜索分页', res);
        if (res.code == 0) {
          let array = []
          array = res.data.list.map(item=>{
            item.html = ''
            console.log(val.param);
            if(val.param){
              item.html = item.title.replace(val.param,"<span style='background:#ff0'>"+val.param+"</span>")
            }else{
              item.html = item.title
            }
            return item
          })
          this.searchList = array
          this.formdata.total = res.data.total
        }
      })
    },
    search(val) {
      this.isClickSearch = true;
      globalQueryAll(val).then(res => {
        // console.log('re点击搜索分页', res);
        if (res.code == 0) {
          console.log(res.data.list);
          let array = []
          array = res.data.list.map(item=>{
            item.html = ''
            if(val.title){
              item.html = item.title.replace(val.title,"<span style='background:#ff0'>"+val.title+"</span>")
            }else{
              item.html = item.title
            }
            return item
          })
          this.searchList = array
          this.formdata.total = res.data.total
        }
      })
    },
    handleSizeChange(val) {
      // console.log("handleSizeChange",val)
      if(this.isClickSearch){// 点击搜索进行查找
        this.formdata.pageSize = val
        this.search(this.formdata)
      }else{
        this.formdata2.pageSize = val
        this.search2(this.formdata2)
      }

    },
    handleCurrentChange(val) {
      // console.log("handleCurrentChange",val)
      if(this.isClickSearch){// 点击搜索进行查找
        this.formdata.pageNum = val
        this.search(this.formdata)
      }else{
        this.formdata2.pageNum = val
        this.search2(this.formdata2)
      }
    },
    open(id) {
      /*this.$router.push({
        name: 'searchcontent',
        query: {id: id }
      })*/
      let routeData = this.$router.resolve({
        name: 'searchcontent',
        query: {id: id }
      })
      window.open(routeData.href, '_blank')
    },
  },
};
</script>
<style scoped>
@import "../assets/css/font/iconfont.css";
@import "../assets/css/index.css";
@import "../assets/css/style.css";
</style>
